<template>
  <div>
    <!-- 面包屑导航栏 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-button type="primary">主要按钮</el-button>

      <!-- 权限列表 -->
      <el-table :data="rolesData" border stripe style="width: 100%">
        <!-- 展开行 -->
        <el-table-column type="expand" label="#">
          <template slot-scope="scope">
            <el-row v-for="(item1,i1) in scope.row.children" :key="item1.id" :class="[i1 ==0? '' :'bor_top','pad_t_b','align_center']">
              <!-- 一级权限 -->
              <el-col :span="5" :class="['justify_center']">
                <el-tag closable>{{item1.authName}}</el-tag>
              </el-col>
              <el-col :span="19">
                <el-row v-for="(item2,i2) in item1.children" :key="item2.id" :class="[i2 ==0? '' :'bor_top','align_center']">
                  <!-- 二级权限 -->
                  <el-col :span="5" :class="['justify_center']">
                    <el-tag type="success" closable>{{item2.authName}}</el-tag>
                  </el-col>
                  <!-- 三级权限 -->
                  <el-col :span="19">
                    <el-tag type="warning" closable v-for="(item3) in item2.children" :key="item3.id" @close="deletePower(scope.row,item3.id)">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <!-- 按钮列 -->
        <el-table-column label="操作" width="300px" ref="testtest">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            <el-button type="info" icon="el-icon-s-help" size="mini" @click="showSetRightDialog(scope.row)">分配权限</el-button>
            <!-- 分配权限点开的dialog对话框 -->
            <el-dialog title="分配权限" :visible.sync="setRightDialogVisible" width="50%" @close="setRightDialogClosed">
              <el-tree :data="rightstree" :props="defaultProps" show-checkbox node-key="id" default-expand-all :default-checked-keys="defKeys" ref="treeRef"></el-tree>
              <span slot="footer" class="dialog-footer">
                <el-button @click="setRightDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="setRoleRight">确 定</el-button>
              </span>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  created() {
    this.requestRolesData();
  },
  data() {
    return {
      // 所有角色列表数据
      rolesData: [],
      // 分配权限dialog对话框显示隐藏
      setRightDialogVisible: false,
      // 所有权限树形数据
      rightstree: [],
      // 树形图配置项
      defaultProps: {
        children: "children",
        label: "authName",
      },
      // 默认选中的节点Id值数组
      defKeys: [],
      roleId: "",
    };
  },
  methods: {
    // 获取角色数据
    requestRolesData() {
      this.$http.get("roles").then((res) => {
        this.rolesData = res.data.data;
      });
    },
    // 展开行删除权限
    deletePower(row, rightId) {
      this.$http.delete(`roles/${row.id}/rights/${rightId}`).then((res) => {
        if (res.data.meta.status == 200) {
          log;
          row.children = res.data.data;
          return this.$message.success("取消权限成功！");
        } else {
          return this.$message.error("取消权限失败！");
        }
      });
    },
    // 分配权限
    showSetRightDialog(roleData) {
      // 存角色id，供更改角色权限时向后端api传送数据
      this.roleId = roleData.id;
      this.$http.get("rights/tree").then((res) => {
        if (res.data.meta.status == 200) {
          // 数据存到data渲染到树形控件
          this.rightstree = res.data.data;
          this.getLeafKeys(roleData);
          //分配权限对话框显示
          this.setRightDialogVisible = true;
          return this.$message.success("获取成功！");
        } else {
          return this.$message.error("获取失败！");
        }
      });
    },
    // 递归获取三级权限id
    getLeafKeys(roleData) {
      if (!roleData.children) {
        this.defKeys.push(roleData.id);
      } else {
        roleData.children.forEach((item) => {
          this.getLeafKeys(item);
        });
      }
    },
    // 关闭分配权限对话框清空id数组,防止叠加
    setRightDialogClosed() {
      this.defKeys = [];
    },
    // 往后台存储新的用户权限
    setRoleRight() {
      // 数组转字符串的新角色权限id
      const newRightId = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
      ].join(",");
      this.$http
        .post(`roles/${this.roleId}/rights`, { rids: newRightId })
        .then((res) => {
          if (res.data.meta.status == 200) {
            // 重新获取角色列表，更新

            this.requestRolesData();
            this.setRightDialogVisible = false;
            return this.$message.success("权限更新成功！");
          } else {
            return this.$message.error("权限更新失败！");
          }
        });
    },
  },
};
</script>

<style>
.pad_t_b {
  padding: 10px 0px;
}
.bor_top {
  border-top: 1px solid #ccc;
}
.el-tag {
  margin: 10px;
}
.align_center {
  display: flex;
  align-items: center;
}
.justify_center {
  display: flex;
  justify-content: center;
}
</style>